<template>
  <div class="chat">
     <div class="title"><van-icon name="arrow-left"  @click="$router.back()" /><p>咨询助手</p></div>
     <div class="chatlist">
      <div v-for="(item ,index) in list" :key="index">
        <div class="content left" v-if="item.name==='xs'">
        <div class="pic"><img src="../../assets/avatar.png" alt=""></div>
        <div class="text">{{item.msg}}</div>
        </div>
        <div class="content right" v-else>
        <div class="text">{{item.msg}}</div>
        <div class="pic"><img src="../../assets/logo.png" alt=""></div>
        </div>
      </div>
     </div>
     <div class="chatInput"><input type="text" placeholder="说点什么..." v-model="word" @keyup.enter="sendFn"><span @click="sendFn">提交</span></div>
  </div>
</template>

<script>
// 导入 socket.io-client 包
import { io } from 'socket.io-client'
export default {
  name: 'Chat',
  data () {
    return {
      word: '', // 输入框的内容
      // 所有的聊天消息
      list: [
        // 只根据 name 属性，即可判断出这个消息应该渲染到左侧还是右侧
        { name: 'xs', msg: 'hi！我是你的贴身租房助手' },
        { name: 'me', msg: '我的房子在哪里' }
      ],
      // 定义变量，存储 websocket 实例
      socket: null
    }
  },
  created() {
    // console.log(2)
    // 接收profile传过来的头像信息
    // this.$bus.$on('sendAvatar', ({ avatar }) => {
    //   console.log(avatar)
    // })
    // console.log('chat')
    // ...
    // 创建客户端 websocket 的实例
    this.socket = io('http://toutiao.itheima.net', {
      query: { token: localStorage.getItem('TOKEN') },
      transports: ['websocket']
    })
    // 建立连接的事件
    this.socket.on('connect', () => {
      // console.log('与服务器建立了连接')
    })
    // 接收到消息的事件
    this.socket.on('message', data => {
      // 把服务器发送过来的消息，存储到 list 数组中
      // console.log(data)
      this.list.push({
        name: 'xs',
        msg: data.msg
      })
      this.$nextTick(() => {
        this.scrollMessage()
      })
    })
  },
  methods: {
    // 向服务端发送消息
    sendFn () {
    // 判断内容是否为空
      if (!this.word.trim()) return
      // 添加聊天消息到 list 列表中
      this.list.push({
        name: 'me',
        msg: this.word
      })
      // 把消息发送给 websocket 服务器
      this.socket.emit('message', {
        msg: this.word,
        timestamp: new Date().getTime()
      })

      // 清空文本框的内容
      this.word = ''
      this.$nextTick(() => {
        this.scrollMessage()
      })
    },
    // 页面滚动到最新发布的消息面前
    scrollMessage() {
      Array.from(document.querySelectorAll('.chatlist .content')).at(-1).scrollIntoView({ behavior: 'smooth' })
    }
  },
  // 组件被销毁之前，清空 sock 对象
  beforeDestroy() {
  // 关闭连接
    this.socket.close()
    // 销毁 websocket 实例对象
    this.socket = null
  }

}
</script>
<style lang="less" scoped>
.chat{
  position: absolute;
  height: 100%;
  width: 100%;
  .title{
  width: 100%;
  height: 45px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  background-color:#43b978;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  color: #FFF;
  .van-icon {
    position: absolute;
    top: 13px;
    left: 15px;
    font-size: 16px;
  }
}
.chatlist{
  padding: 45px 10px;
  width: 100%;
  height: 100%;
  overflow-y: scroll;
  background-color: #fafafa;
  .content{
    margin-top: 15px;
    display: flex;
    .pic{
      width: 45px;
      height: 42px;
      border-radius: 50%;
      img{
      width: 45px;
      height: 42px;
      }
    }
    .text{
      font-size: 14px;
      line-height: 42px;
      border: 0.01333rem solid #c2d9ea;
      border-radius: 0.10667rem;
      position: relative;
      padding: 0 0.26667rem;
      background-color: #e0effb;
       &::after{
        content: '';
        position: absolute;
        width: 16px;
        height: 16px;
        background-color: #e0effb;
        border: 0.01333rem solid #c2d9ea;
      }
    }
  }
  .left{
    .pic{
       margin-right: 20px;
    }
    .text{
       &::after{
        top: 10px;
        left: -8px;
        transform: rotate(45deg);
        border-top: none;
        border-right: none;
       }
    }
  }
  .right{
    justify-content: end;
    .pic{
       margin-left: 20px;
       transform: rotateY(180deg) ;
    }
    .text{
       &::after{
        top: 10px;
        right: -8px;
        transform: rotate(45deg);
        border-bottom: none;
        border-left: none;
       }
    }

  }
}
.chatInput{
  height: 45px;
  width: 100%;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  input{
    flex: 1;
    font-size: 15px;
    outline: none;
    border: none;
    background-color: #fff;
    &::placeholder{
      color: #999;
    }
  }
  span{
    font-size: 13px;
    width: 40px;
    height: 45px;
    background-color: #fff;
    text-align: center;
    line-height: 45px;
    color: #666;
  }
}
}
</style>
